#include "Main.h"

// var multiboard = getScreenHook(string name, int columns, int rows);
INT my_getMultiboard(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	myBoard* pBoard = new myBoard;

	if(!pBoard || IsBadReadPtr(pBoard, sizeof(myBoard)))
		return JS_TRUE;

	pBoard->_dwPrivateType = PRIVATE_MULITBOARD;
	pBoard->pMultiboard = new cMultiboard;

	JSObject* jsunit = JS_NewObject(cx, &multiboard_class, NULL, NULL);
	
	if(!jsunit)
	{
		delete pBoard;
		return JS_TRUE;
	}

	if(!JS_SetPrivate(cx, jsunit, pBoard))	
		return JS_TRUE;

	if(!JS_DefineProperties(cx, jsunit, multiboard_props) || !JS_DefineFunctions(cx, jsunit, multiboard_methods)) 
		return JS_TRUE;

	pBoard->pMultiboard->CreateMultiboard();

	*rval = OBJECT_TO_JSVAL(jsunit);

	return JS_TRUE;
}

VOID multiboard_finalize(JSContext *cx, JSObject *obj)
{
	myBoard* pBoard = (myBoard*)JS_GetPrivate(cx, obj);

	if(pBoard && !IsBadReadPtr(pBoard, sizeof(myBoard)))
	{
		pBoard->pMultiboard->DestroyMultiboard();
		delete pBoard->pMultiboard;
		delete pBoard;
	}
}

INT multiboard_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
	myBoard* pBoard = (myBoard*)JS_GetPrivate(cx, obj);

	if(!pBoard && IsBadReadPtr(pBoard, sizeof(myBoard)))
		return JS_TRUE;

	switch(JSVAL_TO_INT(id))
	{
		case MULTIBOARD_TITLE:
			*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, pBoard->pMultiboard->GetTitleText()));
		break;
		case MULTIBOARD_COLUMNS:
			*vp = INT_TO_JSVAL(pBoard->pMultiboard->GetMultiboardColumnCount());
		break;
		case MULTIBOARD_ROWS:
			*vp = INT_TO_JSVAL(pBoard->pMultiboard->GetMultiboardRowCount());
		break;	
		case MULTIBOARD_DISPLAYED:
			*vp = BOOLEAN_TO_JSVAL(pBoard->pMultiboard->IsMultiboardDisplayed());
		break;
		case MULTIBOARD_MINIMIZED:
			*vp = BOOLEAN_TO_JSVAL(pBoard->pMultiboard->IsMultiboardMinimized());
		break;
		default:
		break;
	}

	return JS_TRUE;
}


INT multiboard_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
	myBoard* pBoard = (myBoard*)JS_GetPrivate(cx, obj);

	if(!pBoard && IsBadReadPtr(pBoard, sizeof(myBoard)))
		return JS_TRUE;

	switch(JSVAL_TO_INT(id))
	{
		case MULTIBOARD_TITLE:
			if(JSVAL_IS_STRING(*vp))
				pBoard->pMultiboard->SetMultiboardTitle(JS_GetStringBytes(JS_ValueToString(cx, *vp)));
		break;
		case MULTIBOARD_COLUMNS:
			if(JSVAL_IS_INT(*vp))
				pBoard->pMultiboard->SetMultiboardColumns(JSVAL_TO_INT(*vp));
		break;
		case MULTIBOARD_ROWS:
			if(JSVAL_IS_INT(*vp))
				pBoard->pMultiboard->SetMultiboardRows(JSVAL_TO_INT(*vp));
		break;
		case MULTIBOARD_TITLECOLOR:
			if(JSVAL_IS_INT(*vp))
				pBoard->pMultiboard->SetMultiboardTitleColor(JSVAL_TO_INT(*vp));
		break;
		case MULTIBOARD_DISPLAYED:
			if(JSVAL_IS_INT(*vp) || JSVAL_IS_BOOLEAN(*vp))
				pBoard->pMultiboard->Display(JSVAL_TO_INT(*vp));
		break;
		case MULTIBOARD_MINIMIZED:
			if(JSVAL_IS_INT(*vp) || JSVAL_IS_BOOLEAN(*vp))
				pBoard->pMultiboard->Minimize(JSVAL_TO_INT(*vp));
		break;
		default:
		break;
	}

	return JS_TRUE;
}

INT multiboard_remove(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	multiboard_finalize(cx, obj);
	*rval = (jsval)NULL;
	return JS_TRUE;
}

INT multiboard_setItemValue(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	myBoard* pBoard = (myBoard*)JS_GetPrivate(cx, obj);

	if(!pBoard && IsBadReadPtr(pBoard, sizeof(myBoard)) || pBoard->_dwPrivateType != PRIVATE_MULITBOARD)
		return JS_TRUE;

	jsint nColumn = 0, nRow = 0;

	if(argc > 1 && JSVAL_IS_INT(argv[0]) && JSVAL_IS_INT(argv[1]) && JSVAL_IS_STRING(argv[2]))
	{
		CHAR szBuffer[1024] = "";		
		sprintf_s(szBuffer, 1024, JS_GetStringBytes(JSVAL_TO_STRING(argv[2])));
		
		nColumn = JSVAL_TO_INT(argv[0]);
		nRow = JSVAL_TO_INT(argv[1]);

		pBoard->pMultiboard->SetItemValue(szBuffer, nColumn, nRow);
	}

	return JS_TRUE;
}

INT multiboard_setItemWidth(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	myBoard* pBoard = (myBoard*)JS_GetPrivate(cx, obj);

	if(!pBoard && IsBadReadPtr(pBoard, sizeof(myBoard)) || pBoard->_dwPrivateType != PRIVATE_MULITBOARD)
		return JS_TRUE;

	jsdouble nWidth = 0;
    jsint nColumn = 0, nRow = 0;

	if(argc > 2 && JSVAL_IS_INT(argv[0]) && JSVAL_IS_INT(argv[1]) && JSVAL_IS_NUMBER(argv[2]))
	{
		nColumn = JSVAL_TO_INT(argv[0]);
		nRow = JSVAL_TO_INT(argv[1]);

		if(JSVAL_IS_INT(argv[2]))
			nWidth = JSVAL_TO_INT(argv[2]);
		else nWidth = *JSVAL_TO_DOUBLE(argv[2]);

		pBoard->pMultiboard->SetItemWidth(nWidth, nColumn, nRow);
	}

	return JS_TRUE;
}

INT multiboard_setItemIcon(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	myBoard* pBoard = (myBoard*)JS_GetPrivate(cx, obj);

	if(!pBoard && IsBadReadPtr(pBoard, sizeof(myBoard)) || pBoard->_dwPrivateType != PRIVATE_MULITBOARD)
		return JS_TRUE;

	jsint nColumn = 0, nRow = 0;

	if(argc > 1 && JSVAL_IS_INT(argv[0]) && JSVAL_IS_INT(argv[1]) && JSVAL_IS_STRING(argv[2]))
	{
		CHAR *lpszText = JS_GetStringBytes(JS_ValueToString(cx, argv[2]));
		
		nColumn = JSVAL_TO_INT(argv[0]);
		nRow = JSVAL_TO_INT(argv[1]);

		pBoard->pMultiboard->SetItemIcon(lpszText, nColumn, nRow);
	}
	
	return JS_TRUE;
}